package li.com.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import li.com.ruiji.common.MyBaseContext;
import li.com.ruiji.common.R;
import li.com.ruiji.dto.DishDto;
import li.com.ruiji.dto.OrdersDto;
import li.com.ruiji.entity.Dish;
import li.com.ruiji.entity.OrderDetail;
import li.com.ruiji.entity.Orders;
import li.com.ruiji.entity.ShoppingCart;
import li.com.ruiji.service.DishService;
import li.com.ruiji.service.OrdersDetailService;
import li.com.ruiji.service.OrdersService;
import li.com.ruiji.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.rmi.server.LoaderHandler;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j
@Api("用户订单操作")
public class OrderController {
    @Autowired
    public OrdersService ordersService;
    @Autowired
    public OrdersDetailService ordersDetailService;
    @Autowired
    public ShoppingCartService shoppingCartService;
    @Autowired
    public DishService dishService;
//    支付订单
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("支付成功");
    }
//    查看订单(用户查看自己的)
    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize){
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        Page<OrdersDto> dtoPage=new Page<>(page,pageSize);
        Long userId= MyBaseContext.getLongId();
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId,userId);
        queryWrapper.orderByDesc(Orders::getCheckoutTime);
        ordersService.page(pageInfo,queryWrapper);
//        弄出来OrderDto对象列表、
        List<OrdersDto> list=pageInfo.getRecords().stream().map((item)->{
            OrdersDto ordersDto = new OrdersDto();
            //获取orderId,然后根据这个id，去orderDetail表中查数据
            Long orderId = item.getId();
            LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(OrderDetail::getOrderId, orderId);
            List<OrderDetail> details = ordersDetailService.list(wrapper);
            BeanUtils.copyProperties(item, ordersDto);
            //之后set一下属性
            ordersDto.setOrderDetails(details);
            return ordersDto;
        }).collect(Collectors.toList());
//        把分页查询的结果复制到需要的分页构造器里，不复制数据部分
        BeanUtils.copyProperties(pageInfo, dtoPage, "records");
        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }
//    查看订单(后台查看所有的订单)
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, Long number, String beginTime, String endTime){
        Page pageInfo = new Page(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Orders::getCheckoutTime);
        if(number!=null){
            queryWrapper.eq(Orders::getId,number);
        }
        if(beginTime!=null){
            queryWrapper.ge(Orders::getCheckoutTime,beginTime);
        }
        if(endTime!=null){
            queryWrapper.le(Orders::getCheckoutTime,endTime);
        }
        Page page1 = ordersService.page(pageInfo, queryWrapper);
        return R.success(page1);
    }
//    修改订单状态
    @PutMapping
    public R<String> update(@RequestBody Orders orders){
        Orders byId = ordersService.getById(orders.getId());
        byId.setStatus(orders.getStatus());
        ordersService.updateById(byId);
        return R.success("操作成功");
    }
//    再来一单
    @PostMapping("/again")
    public R<String> again(@RequestBody Map<String,String> map){
        //获取order_id
        Long orderId = Long.valueOf(map.get("id"));
        //条件构造器
        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        //查询订单的口味细节数据
        queryWrapper.eq(OrderDetail::getOrderId,orderId);
        List<OrderDetail> details = ordersDetailService.list(queryWrapper);
        //获取用户id，待会需要set操作
        Long userId = MyBaseContext.getLongId();
        List<ShoppingCart> shoppingCarts = details.stream().map((item) ->{
            ShoppingCart shoppingCart = new ShoppingCart();
            //Copy对应属性值
            BeanUtils.copyProperties(item,shoppingCart);
            //设置一下userId
            shoppingCart.setUserId(userId);
            //设置一下创建时间为当前时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            return shoppingCart;
        }).collect(Collectors.toList());
        //加入购物车
        shoppingCartService.saveBatch(shoppingCarts);
        return R.success("喜欢吃就再来一单吖~");
    }
}
